# -*- coding:utf-8 -*-
"""
@Author : 审心
@Time : 2023/10/23 17:20
生意参谋页面的一些操作封装
"""
from dataclasses import dataclass

from DrissionPage import WebPage

from common.utils.web_page_util import MockTap


@dataclass
class PageAction:
    p: WebPage

    def wait_load(self, wait_timeout: int = 30):
        """
        等待界面进入加载状态
        :param wait_timeout:
        :return:
        """
        self.p.wait.load_start(wait_timeout)
        return self

    def goto_index(self):
        """
        前往生意参谋首页
        :return: 当前对象
        """
        self.p.get("https://sycm.taobao.com/portal/home.htm")
        return self

    def goto_market(self):
        """
        跳转到 [市场] 标签页
        https://sycm.taobao.com/mc/mq/market_monitor
        :return:
        """
        if self.p.url.startswith("https://sycm.taobao.com/mc/mq/market_monitor"):
            return self
        MockTap(self.p("x://span[.='市场']"), "跳转到 [市场] 标签页失败", before_delay=(1, 3))()
        return self

    def goto_market_rank_item(self):
        """
        跳转到 [市场排行] -> [商品] 标签页
        https://sycm.taobao.com/mc/mq/market_rank?activeKey=item
        :return:
        """
        if self.p.url.startswith("https://sycm.taobao.com/mc/mq/market_rank?activeKey=item"):
            return self
        MockTap(self.p("x://span[.='市场排行']"), "跳转到 [市场排行] 标签页失败", before_delay=(1, 3))()
        self.wait_load(30)
        MockTap(self.p("x://div[@class='ebase-FaCommonFilter-module__bottom']")("tx:商品"),
                "跳转到 [商品] 标签页失败", before_delay=(1, 3), scope_random=False)()
        return self

    def is_login_view(self):
        """
        是否在登录界面
        :return: 是/否
        """
        return "login.html" in self.p.url


if __name__ == '__main__':
    pass
